package com.kykj.kykj.controller;

import com.kykj.kykj.entity.ResultEntity;
import com.kykj.kykj.service.impl.ArticleViewsServiceImpl;
import com.kykj.kykj.vo.ArticleViewsVo;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RequestMapping("v2/articles/")
@CrossOrigin//解决跨域问题
@RestController
public class ArticleViewsController {
    @Resource
    ArticleViewsServiceImpl articleViewsService;

    /**
     * 获取文章浏览列表
     * @return resultEntity
     */
    @GetMapping("getArticleViewsList")
    public ResultEntity getArticleViewsList(){
        ResultEntity resultEntity = new ResultEntity();
        try{
            resultEntity.setData(articleViewsService.getArticleViewsList());
            resultEntity.setCode(0);
            resultEntity.setMsg("文章浏览记录获取成功！");
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    /**
     * 添加文章浏览
     * @param articleViewsVo 浏览记录对象
     * @return resultEntity
     */
    @PostMapping("articleviews")
    public ResultEntity addArticleViews(HttpServletRequest request, @RequestBody ArticleViewsVo articleViewsVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            // 获取客户端IP地址
            String clientIp = getClientIpAddress(request);
            articleViewsVo.setIp(clientIp);

            // 获取并设置访问设备信息
            String deviceType = getDeviceType(request);
            articleViewsVo.setClient(deviceType);

            if(articleViewsService.addArticleViews(articleViewsVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("文章浏览添加成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("文章浏览添加失败！");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            resultEntity.setCode(-9999);
            resultEntity.setMsg("添加失败！");
        }
        return resultEntity;
    }

    // 获取客户端IP地址的方法
    private String getClientIpAddress(HttpServletRequest request) {
        String xffHeader = request.getHeader("X-Forwarded-For");
        if (xffHeader == null) {
            return request.getRemoteAddr();
        }
        return xffHeader.split(",")[0];
    }

    // 判断访问设备类型
    private String getDeviceType(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent").toLowerCase();

        // 判断是否为移动设备
        if (userAgent.contains("mobile") || userAgent.contains("android") ||
                userAgent.contains("iphone") || userAgent.contains("ipad")) {
            return "mobile";
        }

        // 默认返回web
        return "web";
    }

    /**
     * 删除文章浏览
     * @param id 浏览id
     * @return resultEntity
     */
    @DeleteMapping("articleviews/{id}")
    public ResultEntity delArticleViews(@PathVariable("id") int id){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articleViewsService.delArticleViews(id)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("文章浏览删除成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("文章浏览删除失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("删除失败");
        }
        return resultEntity;
    }

    /**
     * 修改文章浏览
     * @param articleViewsVo 文章浏览对象
     * @return resultEntity
     */
    @PutMapping("articleviews")
    public ResultEntity upArticleViews(@RequestBody ArticleViewsVo articleViewsVo){
        ResultEntity resultEntity = new ResultEntity();
        try{
            if(articleViewsService.upArticleViews(articleViewsVo)>0){
                resultEntity.setCode(0);
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode(1);
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return  resultEntity;
    }
}
